package com.crm.service.system.role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.model.common.dtos.PageResponseResult;
import com.crm.model.common.dtos.ResponseResult;
import com.crm.model.common.enums.AppHttpCodeEnum;
import com.crm.model.system.pojo.User;
import com.crm.model.system.role.dto.RoleDto;
import com.crm.model.system.role.dto.RolePageDto;
import com.crm.model.system.role.pojo.Role;
import com.crm.service.system.role.mapper.RoleMapper;
import com.crm.service.system.role.service.RoleService;
import com.crm.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    //新增或修改角色
    @Override
    public ResponseResult save(RoleDto roleDto) {
        //健壮性判断
        if (roleDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (roleDto.getRoleName() == null || roleDto.getCharacters() == null || roleDto.getStatus() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //创建角色对象
        Role role = new Role();
        BeanUtils.copyProperties(roleDto,role);
        //判断是增加还是修改
        if (roleDto.getId() == null){
            //每有id,为增加
            //添加缺少字段
            saveRole(role);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //有id,为修改
        updateRole(role);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //根据id查询角色
    @Override
    public ResponseResult getById(Integer id) {
        //健壮性判断
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //根据id查询
        Role role = roleMapper.selectById(id);
        return ResponseResult.okResult(role);
    }

    //根据id单个或者批量删除角色
    @Override
    public ResponseResult deleteByIds(List<Integer> ids) {
        //健壮性判断
        if (ids == null || ids.size() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        for (Integer id : ids) {
            //判断包含的角色能否被删除
            Role role = roleMapper.selectById(id);
            if (role.getStatus() == 1){
                return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_NOT_ROLE);
            }
        }
        //删除
        roleMapper.deleteBatchIds(ids);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //查询所有或条件查询所有角色
    @Override
    public ResponseResult getAll(RolePageDto rolePageDto) {
        {
            if (rolePageDto == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
            }
            //分页查询
            IPage page = new Page(rolePageDto.getPage(),rolePageDto.getSize());
            //条件分页
            LambdaQueryWrapper<Role> lambdaQueryWrapper = getRoleLambdaQueryWrapper(rolePageDto);
            //打包
            page = page(page,lambdaQueryWrapper);
            ResponseResult responseResult = new PageResponseResult(rolePageDto.getPage(),rolePageDto.getSize(), (int) page.getTotal());
            responseResult.setData(page.getRecords());
            return responseResult;
        }
    }

    //分页条件
    private LambdaQueryWrapper<Role> getRoleLambdaQueryWrapper(RolePageDto rolePageDto) {
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (rolePageDto.getName()!=null && !("").equals(rolePageDto.getName())){
            lambdaQueryWrapper.like(Role::getRoleName, rolePageDto.getName());
        }
        if (rolePageDto.getStatus()!=null && !("").equals(rolePageDto.getStatus())){
            lambdaQueryWrapper.eq(Role::getStatus, rolePageDto.getStatus());
        }
        if (rolePageDto.getCreateTime()!=null && !("").equals(rolePageDto.getCreateTime())){
            lambdaQueryWrapper.eq(Role::getCreateTime, rolePageDto.getCreateTime());
        }
        if (rolePageDto.getCharacters()!=null && !("").equals(rolePageDto.getCharacters())){
            lambdaQueryWrapper.eq(Role::getCharacters, rolePageDto.getCharacters());
        }
        return lambdaQueryWrapper;
    }

    //更新角色
    private void updateRole(Role role) {
        //获取登录用户信息
        User user = ThreadLocalUtil.getUser();
//        role.setUpdatePeople(user.getName());
        role.setUpdatePeople("2");
        role.setUpdateTime(LocalDate.now());
        roleMapper.updateById(role);
    }

    //新增角色
    private void saveRole(Role role) {
        //获取登录的用户信息
        User user = ThreadLocalUtil.getUser();
//        role.setCreatePeople(user.getName());
        role.setCreatePeople("2");
        role.setCreateTime(LocalDate.now());
        roleMapper.insert(role);
    }

}
